package pers.zw.study.servlet;

import com.sun.corba.se.impl.orbutil.ObjectUtility;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Use printWriter to output Chinese
 */
@WebServlet(name = "ResponseDemo2", urlPatterns = "/ResponseDemo2")
public class ServletResponseDemo2 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    private void outputChineseByPrintWriter(HttpServletResponse response) throws IOException {
        String data = "中国";

        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();

        response.setHeader("content-type", "text/html;charset = UTF-8");
        /* 失败了 */
        //out.write("<meta http-equiv = 'content-type' content = 'text/html; charset = UTF-8' />");
        out.write(data);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        outputChineseByPrintWriter(response);
    }
}
